﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.Geodatabase;
using Akiha.Core;
using ESRI.ArcGIS.Geometry;

namespace ArcMap_AddIn_Tools
{
    public partial class UpdateZDHForm : Form
    {
        private IMxDocument mxDocument = ArcMap.Document;
        private IFeatureClass zd_fc = null;
        private string zdh_name = "";
        private string djh_name = "";
        private string tdyt_name = "";

        public UpdateZDHForm()
        {
            InitializeComponent();
        }

        private void UpdateZDHForm_Load(object sender, EventArgs e)
        {
            LoadFeatureLayerToCombox();
        }

        private void LoadFeatureLayerToCombox()
        {
            IMap map = mxDocument.FocusMap;
            if (map.LayerCount == 0)
                return;
            List<IFeatureLayer> fcs = MapWrapper.GetLayers(map, null);
            if (fcs.Count == 0)
                return;
            this.comboBox_ZdFC.BeginUpdate();
            for (int i = 0; i < fcs.Count; i++)
            {
                string name = fcs[i].Name;
                this.comboBox_ZdFC.Items.Add(name);
            }
            this.comboBox_ZdFC.EndUpdate();
        }

        private void comboBox_ZdFC_SelectedIndexChanged(object sender, EventArgs e)
        {
            string zd_point_layer_name = this.comboBox_ZdFC.Text;
            IMap map = mxDocument.FocusMap;
            if (map.LayerCount == 0)
                return;
            List<IFeatureLayer> fcs = MapWrapper.GetLayers(map, zd_point_layer_name);
            if (fcs.Count == 0)
                return;
            IFeatureLayer zd_layer = fcs[0];
            IFields fields = zd_layer.FeatureClass.Fields;
            zd_fc = zd_layer.FeatureClass;
            this.comboBox_ZdZDH.BeginUpdate();
            this.comboBox_ZdDJH.BeginUpdate();
            this.comboBox_ZdTDYT.BeginUpdate();
            for (int i = 0; i < fields.FieldCount; i++)
            {
                IField field = fields.get_Field(i);
                if (field.Editable == true)
                {
                    this.comboBox_ZdZDH.Items.Add(field.Name);
                    this.comboBox_ZdDJH.Items.Add(field.Name);
                    this.comboBox_ZdTDYT.Items.Add(field.Name);
                }
            }
            this.comboBox_ZdZDH.EndUpdate();
            this.comboBox_ZdDJH.EndUpdate();
            this.comboBox_ZdTDYT.EndUpdate();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            IQueryFilter QueryFilter = new QueryFilterClass();
            QueryFilter.SubFields = "*";
            QueryFilter.WhereClause = string.Format("");
            List<IFeature> zdList = FeatureClassWrapper.GetFeaturesFromFeatureClass(zd_fc, QueryFilter);
            for (int i = 0; i < zdList.Count; i++)
            {
                try
                {
                    Dictionary<string, object> attribute = new Dictionary<string, object>();
                    IFeature zdFeature = zdList[i];
                    string zdh_new = "";
                    string djh_new = "";
                    string tdyt_new = "";

                    if (this.comboBox_ZdZDH.Text != "")
                    {
                        zdh_name = this.comboBox_ZdZDH.Text;
                        string zdh_old = FeatureWrapper.GetFieldValue(zdFeature, zdh_name).ToString();
                        zdh_new = zdh_old.Substring(0, zdh_old.Length - 4);
                        int zdh_zh = MathWrapper.FormatStringToIntByDelFirstZero(zdh_old.Substring(zdh_old.Length - 4));
                        //zdh_new = MathWrapper.FormatIntToStringAddZero(zdh_new, 4) + "000";
                        zdh_new = MathWrapper.FormatIntToStringAddZero(zdh_new, 4) +
                            MathWrapper.FormatIntToStringAddZero(zdh_zh.ToString(), 3);
                        attribute.Add(zdh_name, zdh_new);
                    }

                    if (this.comboBox_ZdDJH.Text != "")
                    {
                        djh_name = this.comboBox_ZdDJH.Text;
                        string djh_old = FeatureWrapper.GetFieldValue(zdFeature, "LOCALID").ToString();
                        djh_new = djh_old.Substring(2);
                        djh_new = "360725" + djh_new + zdh_new;
                        attribute.Add(djh_name, djh_new);
                    }

                    if (this.comboBox_ZdTDYT.Text != "")
                    {
                        tdyt_name = this.comboBox_ZdTDYT.Text;
                        string tdyt_old = FeatureWrapper.GetFieldValue(zdFeature, tdyt_name).ToString();
                        tdyt_new = GetTdytBmFromTdytName(tdyt_old);
                        attribute.Add(tdyt_name, tdyt_new);
                    }

                    FeatureClassWrapper.UpdateFeatureValues(zd_fc, zdFeature, attribute);
                    string txt = string.Format("进度：{0}/{1}", i + 1, zdList.Count);
                    this.label_JD.Text = txt;
                    Application.DoEvents();
                }
                catch (Exception ex)
                {
                    continue;
                }

            }
        }

        private string GetTdytBmFromTdytName(string tdyt_name)
        {
            //***********************************************************************//
            //GB/T21010-2007《土地利用现状分类》

            //仓储用地               063   仓储用地
            //耕地                   01    耕地
            //工业用地               061   工业用地
            //公共基础设施用地       086   公共设施用地
            //果园                   021   果园
            //机关团体用地           081   机关团体用地
            //街巷                   103   街巷用地
            //金融保险用地           053   商务金融用地
            //军事设施用地           091   军事设施用地
            //商服用地               05    商服用地
            //文体用地               085   文体娱乐用地
            //医疗卫生用地           084   医卫慈善用地
            //住宅用地               07    住宅用地
            //农村宅基地             072   农村宅基地


            //教育用地               083   科教用地
            //科研设计用地           083   科教用地

            //城镇单一住宅用地       071   城镇住宅用地
            //城镇混合住宅用地       071   城镇住宅用地


            //商业用地              053   商务金融用地
            //瞻仰景观休闲用地
            //综合用地
            //***********************************************************************//
            string tdytBm = tdyt_name;
            switch (tdyt_name)
            {
                case "":
                    tdytBm = "";
                    break;
                case "仓储用地":
                    tdytBm = "063";
                    break;
                case "耕地":
                    tdytBm = "01";
                    break;
                case "工业用地":
                    tdytBm = "061";
                    break;
                case "公共基础设施用地":
                    tdytBm = "086";
                    break;
                case "果园":
                    tdytBm = "021";
                    break;
                case "机关团体用地":
                    tdytBm = "081";
                    break;
                case "街巷":
                    tdytBm = "103";
                    break;
                case "金融保险用地":
                    tdytBm = "053";
                    break;
                case "军事设施用地":
                    tdytBm = "091";
                    break;
                case "商服用地":
                    tdytBm = "05";
                    break;
                case "文体用地":
                    tdytBm = "085";
                    break;
                case "医疗卫生用地":
                    tdytBm = "084";
                    break;
                case "住宅用地":
                    tdytBm = "07";
                    break;
                case "农村宅基地":
                    tdytBm = "072";
                    break;
                case "教育用地":
                    tdytBm = "083";
                    break;
                case "科研设计用地":
                    tdytBm = "083";
                    break;
                case "城镇单一住宅用地":
                    tdytBm = "071";
                    break;
                case "城镇混合住宅用地":
                    tdytBm = "071";
                    break;
                case "商业用地":
                    tdytBm = "053";
                    break;
                case "瞻仰景观休闲用地":
                    tdytBm = "205";
                    break;
                case "综合用地":
                    tdytBm = "12";
                    break;
            }

            return tdytBm;
        }
    }
}
